<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户类型" prop="companyType">
        <el-select v-model="queryParams.companyType" placeholder="请选择客户类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.company_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          maxlength="11"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户状态" prop="companystatus">
        <el-select v-model="queryParams.companystatus" placeholder="请选择客户状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.company_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customer:customer:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['customer:customer:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['customer:customer:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customer:customer:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id"/>
      <el-table-column width="150" label="客户名称" align="center" prop="companyName"/>
      <el-table-column label="客户类型" align="center" prop="companyType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.company_type" :value="scope.row.companyType"/>
        </template>
      </el-table-column>
      <el-table-column label="客户状态" align="center" prop="companystatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.company_status" :value="scope.row.companystatus"/>
        </template>
      </el-table-column>
      <el-table-column label="所属行业" align="center" prop="industryname"/>
      <el-table-column label="联系人" align="center" prop="name"/>
      <el-table-column width="110" label="手机号话" align="center" prop="mobile"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['customer:customer:details']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['customer:customer:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customer:customer:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入客户名称"/>
        </el-form-item>
        <el-form-item label="客户类型" prop="companyType">
          <el-select v-model="form.companyType" placeholder="请选择客户类型">
            <el-option
              v-for="dict in dict.type.company_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属国家" prop="companyCountry">
          <el-input v-model="form.companyCountry" placeholder="请输入所属国家"/>
        </el-form-item>
        <el-form-item label="统一信用代码" prop="socialCreditCode">
          <el-input v-model="form.socialCreditCode" placeholder="请输入统一信用代码"/>
        </el-form-item>
        <el-form-item label="所属行业" prop="industryname">
          <el-input v-model="form.industryname" placeholder="请输入所属行业"/>
        </el-form-item>
        <el-form-item label="注册地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入注册地址"/>
        </el-form-item>
        <el-form-item label="联系人" prop="name">
          <el-input v-model="form.name" placeholder="请输入联系人"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" maxlength="11"/>
        </el-form-item>
        <el-form-item label="联系邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入联系邮箱"/>
        </el-form-item>
        <el-form-item label="职务" prop="post">
          <el-input v-model="form.post" placeholder="请输入职务"/>
        </el-form-item>
        <el-form-item label="规模" prop="scale">
          <el-input v-model="form.scale" placeholder="请输入规模"/>
        </el-form-item>
        <el-form-item label="通讯地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入通讯地址"/>
        </el-form-item>
        <el-form-item label="营业范围" prop="businessScope">
          <el-input v-model="form.businessScope" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="客户状态" prop="companyStatus">
          <el-select v-model="form.companyStatus" placeholder="请选择客户类型">
            <el-option
              v-for="dict in dict.type.company_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入孵时间" prop="incubatorTime">
          <el-date-picker clearable size="small"
                          v-model="form.incubatorTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择入孵时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="注册资金" prop="registerMoney">
          <el-input v-model="form.registerMoney" placeholder="请输入注册资金"/>
        </el-form-item>
        <el-form-item label="注册成立时间" prop="registerTime">
          <el-date-picker clearable size="small"
                          v-model="form.registerTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择注册成立时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="registerendTime">
          <el-date-picker clearable size="small"
                          v-model="form.registerendTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="技术领域" prop="technicalField">
          <el-input v-model="form.technicalField" placeholder="请输入技术领域"/>
        </el-form-item>
        <el-form-item label="是否优质企业" prop="hot">
          <el-input v-model="form.hot" placeholder="请输入是否优质企业"/>
        </el-form-item>
        <el-form-item label="客户图片">
          <imageUpload v-model="form.companyLogo"/>
        </el-form-item>
        <el-form-item label="客户图片集">
          <imageUpload v-model="form.companyImgs" :limit="8"/>
        </el-form-item>
        <el-form-item label="经营状态" prop="managementType">
          <el-input v-model="form.managementType" placeholder="请输入经营状态"/>
        </el-form-item>
        <el-form-item label="企业性质" prop="companyNature">
          <el-input v-model="form.companyNature" placeholder="请输入企业性质"/>
        </el-form-item>
        <el-form-item label="登记机关" prop="registrationAuthority">
          <el-input v-model="form.registrationAuthority" placeholder="请输入登记机关"/>
        </el-form-item>
        <el-form-item label="法人" prop="legalPerson">
          <el-input v-model="form.legalPerson" placeholder="请输入法人"/>
        </el-form-item>
        <el-form-item label="客户描述" prop="companyDesc">
          <el-input v-model="form.companyDesc" type="textarea" placeholder="请输入内容"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer} from "@/api/customer/customer";

  export default {
    name: "Customer",
    dicts: ['sys_status', 'company_type', 'company_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户表格数据
        customerList: [],
        optioncustomerList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          companyType: null,
          companyName: null,
          companyCountry: null,
          createTime: null,
          email: null,
          industryname: null,
          industryId: null,
          mobile: null,
          name: null,
          post: null,
          scale: null,
          address: null,
          status: null,
          updateTime: null,
          businessScope: null,
          companystatus: null,
          incubatorTime: null,
          registerMoney: null,
          registerTime: null,
          socialCreditCode: null,
          technicalField: null,
          lnglat: null,
          hot: null,
          companyLogo: null,
          companyImgs: null,
          registerendTime: null,
          managementType: null,
          companyNature: null,
          registrationAuthority: null,
          legalPerson: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          companyName: [
            {required: true, message: "客户名称不能为空！", trigger: "blur"}
          ],
          companyType: [
            {required: true, message: "客户类型不能为空！", trigger: "blur"}
          ],
          name: [
            {required: true, message: "联系人不能为空！", trigger: "blur"}
          ],
          mobile: [{
            validator: this.conductionData.commonFun.checkPhone,
            trigger: 'blur',
            required: true,
          }]
        }
      };
    },
    activated() {
      this.getList();
    },
    methods: {
      /** 查询客户列表 */
      getList() {
        this.loading = true;
        listCustomer(this.queryParams).then(response => {
          this.customerList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          companyAddress: null,
          companyType: null,
          companyName: null,
          companyCountry: null,
          companyDesc: null,
          createTime: null,
          email: null,
          industryname: null,
          industryId: null,
          mobile: null,
          name: null,
          post: null,
          scale: null,
          address: null,
          status: 0,
          updateTime: null,
          createBy: null,
          updateBy: null,
          businessScope: null,
          companystatus: "0",
          incubatorTime: null,
          registerMoney: null,
          registerTime: null,
          socialCreditCode: null,
          technicalField: null,
          lnglat: null,
          hot: null,
          companyLogo: null,
          companyImgs: null,
          registerendTime: null,
          managementType: null,
          companyNature: null,
          registrationAuthority: null,
          legalPerson: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 详情
      handleDetail(row) {
        this.conductionData.customerDetails = row;
        this.$router.push({
          path: "/customer/CustomerDetails"
        })
      },
      /** 新增按钮操作 */
      handleAdd() {
        /*this.reset();
        this.open = true;
        this.title = "添加客户";*/
        this.$router.push({
          path: "/customer/CustomerUpdate"
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        /*this.reset();
        const id = row.id || this.ids
        getCustomer(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改客户";
        });*/
        this.$router.push({
          path: "/customer/CustomerUpdate",
          query:{
            id:row.id || this.ids
          }
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateCustomer(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCustomer(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }else{
            this.$modal.msgError("请输入正确格式信息！");
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除客户编号为"' + ids + '"的数据项？').then(function () {
          return delCustomer(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('customer/customer/export', {
          ...this.queryParams
        }, `customer_${new Date().getTime()}.xlsx`)
      },
      // 楼宇下拉聚焦
      remoteMethods() {
        this.optioncustomerList = this.customerList;
      },
      // 楼宇下拉搜索选择
      remoteMethod(query) {
        if (query !== '') {
          this.optioncustomerList = this.customerList.filter(item => {
            return item.companyName.toLowerCase().indexOf(query.toLowerCase()) > -1;
          });
        } else {
          this.optioncustomerList = this.customerList;
        }
      },
    }
  };
</script>
